package com.erebus.login.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.erebus.base.model.RestResponse;
import com.erebus.login.mapper.StudentMapper;
import com.erebus.login.mapper.TeacherMapper;
import com.erebus.login.mapper.UserMapper;
import com.erebus.login.model.dto.UserDto;
import com.erebus.login.model.po.Student;
import com.erebus.login.model.po.Teacher;
import com.erebus.login.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


//账号相关实现类
@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private TeacherMapper teacherMapper;



    //登录
    @Override
    public RestResponse<String> Login(UserDto userDto) {


        // 判断类型
        if (userDto.getCategory() == 0) { // 学生
            Student student = studentMapper.selectOne(new LambdaQueryWrapper<Student>().eq(Student::getUserName, userDto.getUsername()));
            //查询用户不存在, 返回null,
            if (student == null) {
                return RestResponse.validfail("账号错误");
            }
            //密码是否正确
            //查询到用户 拿到正确密码
            String passwordDb = student.getPassword();
            //拿到用户输入的密码
            String passwordForm = userDto.getPassword();
            //校验密码
            if (!passwordDb.equals(passwordForm)) {
                return RestResponse.validfail("密码错误");
            }

            return RestResponse.success(student.getStudentId(), "登录成功");
        }

        Teacher teacher = teacherMapper.selectOne(new LambdaQueryWrapper<Teacher>().eq(Teacher::getUserName, userDto.getUsername()));
        //查询用户不存在, 返回null,
        if (teacher == null) {
            return RestResponse.validfail("账号错误");
        }
        //密码是否正确
        //查询到用户 拿到正确密码
        String passwordDb = teacher.getPassword();
        //拿到用户输入的密码
        String passwordForm = userDto.getPassword();
        //校验密码
        if (!passwordDb.equals(passwordForm)) {
            return RestResponse.validfail("密码错误");
        }

        return RestResponse.success(teacher.getTeacherId(), "登录成功");
    }

}
